package com.my_musc_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my_musc_service.dao.FavoriteDao;
import com.my_musc_service.dao.SharedSongDao;
import com.my_musc_service.dao.SharedSongVODao;
import com.my_musc_service.entity.Favorite;
import com.my_musc_service.entity.SharedSong;
import com.my_musc_service.entity.SharedSongVO;
import com.my_musc_service.entity.User;
import com.my_musc_service.exception.CustomException;
import com.my_musc_service.interceptor.LoginInterceptor;
import com.my_musc_service.service.SharedSongService;
import com.my_musc_service.utils.JudgeFlagUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (SharedSong)表服务实现类
 *
 * @author A.Dun
 * @since 2021-08-17 14:14:27
 */
@Service("sharedSongService")
public class SharedSongServiceImpl extends ServiceImpl<SharedSongDao, SharedSong> implements SharedSongService {
    @Resource
    private SharedSongDao sharedSongDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SharedSong queryById(Integer id) {
        return this.sharedSongDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SharedSong> queryAllByLimit(int offset, int limit) {
        return this.sharedSongDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param sharedSong 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insert(SharedSong sharedSong) {
        int count = this.sharedSongDao.insert(sharedSong);
        return JudgeFlagUtils.isChanged(count);
    }

    /**
     * 修改数据
     *
     * @param sharedSong 实例对象
     * @return 实例对象
     */
    @Override
    public SharedSong update(SharedSong sharedSong) {
        this.sharedSongDao.update(sharedSong);
        return this.queryById(sharedSong.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sharedSongDao.deleteById(id) > 0;
    }

    @Override
    public Boolean addClickCount(SharedSong sharedSong) {
        List<SharedSong> sharedSongs = baseMapper.queryAll(sharedSong);
        if (!CollectionUtils.isEmpty(sharedSongs)) {
            SharedSong sharedSongData = sharedSongs.get(0);
            Integer clickCount = sharedSongData.getClickCount();
            if (clickCount != null) {
                clickCount += 1;
            } else {
                clickCount = 0;
                clickCount += 1;
            }
            sharedSongData.setClickCount(clickCount);
            int count = baseMapper.update(sharedSong);
            return JudgeFlagUtils.isChanged(count);
        } else {
            throw new CustomException("更新失败");
        }
    }

    @Override
    public Boolean delClickCount(SharedSong sharedSong) {
        List<SharedSong> sharedSongs = baseMapper.queryAll(sharedSong);
        if (!CollectionUtils.isEmpty(sharedSongs)) {
            SharedSong sharedSongData = sharedSongs.get(0);
            Integer clickCount = sharedSongData.getClickCount();
            if (clickCount != null && clickCount > 0) {
                clickCount -= 1;
            }
            sharedSongData.setClickCount(clickCount);
            int count = baseMapper.update(sharedSong);
            return JudgeFlagUtils.isChanged(count);
        } else {
            throw new CustomException("更新失败");
        }
    }

    @Autowired
    private SharedSongVODao sharedSongVODao;

    @Autowired
    private FavoriteDao favoriteDao;

    @Override
    public IPage<SharedSongVO> selectPage(Page<SharedSongVO> songPageParam, SharedSong sharedSong) {
//        baseMapper.selectPage(songPageParam, null);
        Page<SharedSongVO> sharedSongVOPage = sharedSongVODao.selectSharedSongPage(songPageParam, null);
        return sharedSongVOPage;
    }

    @Override
    public Map<String, Object> selectSharedSongVOData(Page<SharedSongVO> songPageParam) {
//        baseMapper.selectPage(songPageParam, null);
        Page<SharedSongVO> sharedSongVOPage = sharedSongVODao.selectSharedSongPage(songPageParam, null);

        long total = sharedSongVOPage.getTotal();
        //获取当前用户
        User userInfo = LoginInterceptor.getUserInfo();
        //获取最爱
        List<Favorite> favoriteList = favoriteDao.selectList(new QueryWrapper<Favorite>().eq("user_id", userInfo.getId()));
        List<String> songIdList = favoriteList.stream().map(Favorite::getSongId).collect(Collectors.toList());
        List<SharedSongVO> sharedSongVOList = sharedSongVOPage.getRecords();
        for (SharedSongVO sharedSongVO : sharedSongVOList) {
            Integer songId = sharedSongVO.getSongId();
            if (songIdList.contains(songId)) {
                sharedSongVO.setIsFavorite(true);
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("sharedSongTotal", total);
        result.put("sharedSongList", sharedSongVOList);
        return result;
    }


}
